//定义变量a、b、c,分别用来获取用户名，用户密码和用户角色的值
var a,b,c;

//用户框失去焦点后验证value值
function oBlur_1() {
     a = document.getElementById("name").value;
    if (a == "") { //用户框value值为空
        document.getElementById("remind_1").innerHTML = "用户名不能为空！".fontcolor("red");
        document.getElementById("change_margin_1").style.marginBottom = 1 + "px";
    } else { //用户框value值不为空
        document.getElementById("remind_1").innerHTML = "";
        document.getElementById("change_margin_1").style.marginBottom = 19 + "px";
    }
}

//密码框失去焦点后验证value值
function oBlur_2() {
    b = document.getElementById("password").value;
    if (b=="") { //密码框value值为空
        document.getElementById("remind_2").innerHTML = "密码不能为空！".fontcolor("red");
        document.getElementById("change_margin_2").style.marginBottom = 1 + "px";
    } else { //密码框value值不为空
        document.getElementById("remind_2").innerHTML = "";
        document.getElementById("change_margin_2").style.marginBottom = 19 + "px";
    }
}

//用户角色框失去焦点后验证value值
function oBlur_3() {
    var myselect=document.getElementById("classification");
    var index=myselect.selectedIndex;
    c=myselect.options[index].value;
//    console.log(c);
    if (c=="") { //角色框value值为空
        document.getElementById("remind_3").innerHTML = "请选择用户角色！".fontcolor("red");
        document.getElementById("change_margin_3").style.marginBottom = 1 + "px";
    } else { //角色框value值不为空
        document.getElementById("remind_3").innerHTML = "";
        document.getElementById("change_margin_3").style.marginBottom = 19 + "px";
    }
}

//用户框获得焦点的隐藏提醒
function oFocus_1() {
    document.getElementById("remind_1").innerHTML = "";
    document.getElementById("change_margin_1").style.marginBottom = 19 + "px";
}

//密码框获得焦点的隐藏提醒
function oFocus_2() {
    document.getElementById("remind_2").innerHTML = "";
    document.getElementById("change_margin_2").style.marginBottom = 19 + "px";
}

//角色框获得焦点的隐藏提醒
function oFocus_3() {
    document.getElementById("remind_3").innerHTML = "";
    document.getElementById("change_margin_3").style.marginBottom = 19 + "px";
}

//若输入框为空，阻止表单的提交
/*function submitTest() {

    if (a=="" && b==""&&c=="") { //用户框value值、密码框value值和角色框的value值都为空
        document.getElementById("remind_1").innerHTML = "用户名不能为空！".fontcolor("red");
        document.getElementById("change_margin_1").style.marginBottom = 1 + "px";
        document.getElementById("remind_2").innerHTML = "密码不能为空！".fontcolor("red");
        document.getElementById("change_margin_2").style.marginBottom = 1 + "px";
        document.getElementById("remind_3").innerHTML = "请选择！".fontcolor("red");
        document.getElementById("change_margin_3").style.marginBottom = 1 + "px";
        return false; //只有返回true表单才会提交
    } else if (a="") { //用户框value值为空
        document.getElementById("remind_1").innerHTML = "用户名不能为空！".fontcolor("red");
        document.getElementById("change_margin_1").style.marginBottom = 1 + "px";
        return false;
    } else if (b="") { //密码框值为空入密码
        document.getElementById("remind_2").innerHTML = "密码不能为空！".fontcolor("red");
        document.getElementById("change_margin_2").style.marginBottom = 1 + "px";
        return false;
    }else if (c=""){//角色框值为空
        document.getElementById("remind_3").innerHTML = "请选择用户角色！".fontcolor("red");
        document.getElementById("change_margin_3").style.marginBottom = 1 + "px";
        return false;
    }
    return true;
}*/

window.onload = function(){

    var index = parent.layer.getFrameIndex(window.name);

    $('#saveUser').click(function () {
        /*if(!submitTest())
            parent.layer.confirm('信息未全，不能添加！',{btn:['确定']});
        else {*/
            var name = $("#name").val();
            var password = $("#password").val();
            var affiliation = $("#company_name").val();
            var classification = $("#classification").val();
            var email = $("#email").val();


            if(name==""||password==""||classification==""){
                parent.layer.confirm('关键信息未全，不能添加！',{btn:['确定']});
            }else {
                $.ajax({
                    url: constUrl + "UserServlet",
                    type: 'post',
                    data: {
                        operateType: "add",
                        userName: name,
                        userPassword: password,
                        userAffiliation: affiliation,
                        userRole: classification,
                        userEmail: email
                    },
                    dataType: 'json',
                    success: function (dataText) {
                        console.log(dataText.result);
                        if (dataText.result) {
                            parent.layer.confirm('添加成功！', {
                                btn: ['确定']
                            }, function () {
                                parent.location.reload(index);
                                parent.layer.close(index);
                            })
                        }else {
                            parent.layer.confirm('用户名已存在！', {
                                btn: ['确定']
                            }, function () {
                                parent.location.reload(index);
                                parent.layer.close(index);
                            })
                        }
                    },

                    complete: function () {
                    },
                    error: function () {

                    }
                });
            }
    });

    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
    $.ajax({
        url:constUrl+"RoleServlet",
        type:'post',
        data:{
            operateType:"showAll"
        },
        dataType:'json',
        success: function(responseText) {
            var role ='';
            for(var i=0;i<responseText.result.roles.length;i++){
                role+='<option value="'+(responseText.result.roles[i].id)+'" >'+responseText.result.roles[i].name+'</option>'
            }
            $("#classification").append(role);
        },
        complete: function() {
        },
        error: function() {
        }
    });
    $.ajax({
        url:constUrl+"CompanyServlet",
        type:'post',
        data:{
            operateType:"showAll",
            onePageCount:"10",
            currentPage:"1"
        },
        dataType:'json',
        success: function(responseText) {
            var companyHtml ='';
            for(var i=0;i<responseText.datas.length;i++){
                companyHtml+='<option value="'+(responseText.datas[i].companyId)+'" >'+responseText.datas[i].companyName+'</option>'

            }
            $("#company_name").append(companyHtml);
        },
        complete: function() {
        },
        error: function() {
        }
    });
}

